package kr.pfg.applications.captureyourscreen.display
{
	import flash.desktop.Clipboard;
	import flash.desktop.NativeDragManager;
	import flash.desktop.NativeDragOptions;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getDefinitionByName;
	
	import kr.as3.display.ResizeAlign;
	import kr.as3.display.ResizeHelper;
	import kr.as3.display.ResizeScale;
	import kr.pfg.applications.captureyourscreen.utils.CapturedImageSaver;

	public class EditFrame extends Sprite
	{
		// things on stage
		public var resizeIcon: Sprite;
		public var resizeIconDummy: Sprite;
		public var canvas: Sprite;
		public var bg: Sprite;
		public var dragBar: Sprite;
		
		private var _fullScreenMode:Boolean;
		
		public function EditFrame()
		{
			//this.resizeIconDummy.buttonMode = true;
			//this.resizeIconDummy.addEventListener( MouseEvent.MOUSE_DOWN, resizeStart );
			this._fullScreenMode = false;
			this.dragBar.buttonMode = true;
		
			this.initResize();
		}
		
		private var resizeBound: Rectangle;
		private var resizeDummyMargin: Point;
		private var resizer: ResizeHelper;
		private function initResize(): void
		{
			this.resizer = new ResizeHelper( this );
			this.resizer.addPosition( this.resizeIcon, ResizeAlign.RIGHT_BOTTOM );
			this.resizer.addPosition( this.resizeIconDummy, ResizeAlign.RIGHT_BOTTOM );
			this.resizer.addScale( this.canvas, ResizeScale.XY );
			this.resizer.addScale( this.bg, ResizeScale.XY );
			this.resizer.addScale( this.dragBar, ResizeScale.XY );
			
			this.resizeDummyMargin = new Point( this.width - this.resizeIconDummy.x, this.height - this.resizeIconDummy.y );
			
			const BOUND_MIN: Point = new Point( 100, 60 );
			const BOUND_MAX: Point = new Point( 800, 600 );
			
			this.resizeBound = new Rectangle( 
																BOUND_MIN.x + this.width - this.canvas.width - this.resizeDummyMargin.x, 	// x
																BOUND_MIN.y + this.height - this.canvas.height - this.resizeDummyMargin.y, 	// y
																BOUND_MAX.x - BOUND_MIN.x, 																// width
																BOUND_MAX.y - BOUND_MIN.y																	// height
															);
		}
		
		private function resizeStart( e: MouseEvent ): void
		{
			this.resizeIconDummy.startDrag( false, this.resizeBound );
			
			this.addEventListener( Event.ENTER_FRAME, resizeListener );
			this.stage.addEventListener( MouseEvent.MOUSE_UP, resizeStop );
			this.stage.addEventListener( Event.MOUSE_LEAVE, resizeStop );
		}
		
		private function resizeListener( e: Event ): void
		{
			this.resizer.resize( this.resizeDummyMargin.x + this.resizeIconDummy.x, this.resizeDummyMargin.y + this.resizeIconDummy.y );
			
			// debug canvas size.
			// trace( this.canvas.width + "x" + this.canvas.height );
		}
		
		private function resizeStop( e: Event ): void
		{
			this.stage.removeEventListener( MouseEvent.MOUSE_UP, resizeStop );
			this.stage.removeEventListener( Event.MOUSE_LEAVE, resizeStop );
			this.removeEventListener( Event.ENTER_FRAME, resizeListener );
			
			this.resizeIcon.stopDrag();
		}
		
		// nike : Temparary save a JPG file.
		
		private var clipboard:Clipboard;
		private var draggableIcon:Sprite;
		private function tempararySaveClipboard():void
		{
			trace( 'tempararySave!!' );
			this.clipboard = CapturedImageSaver.getToSaveAsJPGClipboard( this._bitmap, "image.jpg" );
			
			if( !this.draggableIcon )
			{
				this.draggableIcon = new Sprite();
				this.draggableIcon.graphics.beginFill( 0, 0 );
				this.draggableIcon.graphics.drawRect( this.canvas.x + 1, this.canvas.y + 1, this._bitmap.width, this._bitmap.height );
				this.draggableIcon.graphics.endFill();
				this.draggableIcon.buttonMode = true;
			}
			this.draggableIcon.addEventListener( MouseEvent.MOUSE_DOWN, this.dragOutHandler );
			this.addChild( this.draggableIcon );
		}
		
		private function dragOutHandler( e:MouseEvent ):void
		{
			this.draggableIcon.removeEventListener( MouseEvent.MOUSE_DOWN, this.dragOutHandler );
			this.removeChild( this.draggableIcon );
			
			var dragIcon:BitmapData = this.loadBmpAssets( "icon_add" );
			var options:NativeDragOptions = new NativeDragOptions();
			options.allowCopy = false;
			options.allowLink = false;
			
			NativeDragManager.doDrag( draggableIcon, this.clipboard, dragIcon, null, options );
			this.draggableIcon.addEventListener( NativeDragEvent.NATIVE_DRAG_COMPLETE, this.dragOutCompletehandler );
			
		}
		
		// nike : When drag out complete..
		private function dragOutCompletehandler( e:NativeDragEvent ):void
		{
			var target:Sprite = e.target as Sprite;
			target.removeEventListener( NativeDragEvent.NATIVE_DRAG_COMPLETE, this.dragOutCompletehandler );
			this.dispatchEvent( e );
		}
		
		// nike : Load FLA file library assets.
		private function loadBmpAssets( name:String ):BitmapData
		{
	 		var referClass:Class = getDefinitionByName( name ) as Class;
			var bmd:BitmapData = new referClass( -1, -1 );
		 	
			return bmd;
		}
		
		public function set fullScreenMode( bool:Boolean ):void
		{
			this._fullScreenMode = bool;
		}
		
		// nike : 
		private var _bitmap: BitmapData;
		public function set bitmap( $bitmap: BitmapData ): void
		{
			var bitmap:Bitmap;
			this._bitmap = $bitmap;
			
			switch( this._fullScreenMode )
			{
				case true :
					{
						bitmap = new Bitmap( this._bitmap );
						bitmap.x = 0
						bitmap.y = 0
						this.width = bitmap.width;
						this.height = bitmap.height + 22;
					}
					break;
				
				case false :
					{
						bitmap = new Bitmap( this._bitmap );
						bitmap.x = this.canvas.x + 1;
						bitmap.y = this.canvas.y + 1;
						this.width = bitmap.width + 6;
						this.height = bitmap.height + 47;
					}
					break;
			}
			
			this.addChild( bitmap );
			this.tempararySaveClipboard();
		}
		
		public function get bitmap(): BitmapData
		{
			return this._bitmap;
		}
		
		// override width and height to resize with resizer
		override public function set width( value: Number ): void
		{
			this.resizer.resize( int( value ), int( this.height ) );
		}
		
		override public function set height( value: Number ): void
		{
			this.resizer.resize( int( this.width ), int( value ) );
		}
	}
}